#pragma once
#include "DynamicFunctionalEntity.h"
#include "al_IAbilityOwner.h"
#include "al_IAbilityInstance.h"
#include "UnitStats.h"
#include <vector>
#include <string>
#include <iostream>

namespace game
{

	enum UnitState
	{
		S_UNIT_MOVING,
		S_UNIT_IDLE,
		S_UNIT_DEAD,
	};


	class Unit:public DynamicFunctionalEntity<UnitState>, public ability::IAbilityOwner
	{
		UnitStats * _max_stats;
		UnitStats * _current_stats;

		//ability_list//
		//std::vector<ability_core::IAbilityInstance
		//int _mp;

	public:
		Unit(UnitState state,sf::Vector2f pos, animSprite * sprite, sf::RenderWindow * window, std::string name, int object_type_id, game::TerrainManager* tm)
			:DynamicFunctionalEntity(state,pos,sprite,window,name,object_type_id,tm)
		{
			_current_stats = new UnitStats(100,100,10);
			_max_stats = new UnitStats(100,100,10);
		}

		UnitStats  & get_max_stats(){return *_max_stats;}
		UnitStats  & get_current_stats(){return *_current_stats;}

		void OnDeath()
		{
			//this->get_anim_sprite()->
			set_state(UnitState::S_UNIT_DEAD);
		}

		void Draw()
		{
			DynamicFunctionalEntity<UnitState>::Draw();
			double current_hp = get_current_stats().getHp();
			double max_hp	 =  get_max_stats().getHp();
			double current_hp_percent =   current_hp / max_hp;
			current_hp_percent *= 100.0;
			sf::RectangleShape rInside(sf::Vector2f(current_hp_percent/10.0f*1.40f,4));
			sf::RectangleShape rOutside(sf::Vector2f(16,6));
			int x = this->get_position().x;
			int y = this->get_position().y;
			rInside.setPosition(x+1,y+1);
			rOutside.setPosition(x,y);
			sf::Uint8 red =(int)( current_hp_percent*2.54);//(int)(10*2.54);

			rInside.setFillColor(sf::Color(255-red,255,0,128));
			rOutside.setFillColor(sf::Color(252,25,25,128));

			get_render_window()->draw(rOutside);
			get_render_window()->draw(rInside);
		}

		//IAbilityMembers
		int		get_mp(){return get_current_stats().getMp();}
		void	add_mp(int amount, int total){get_current_stats().addMp(amount,total);}

		virtual void Init()
		{
			DynamicFunctionalEntity::Init();
			//Load();
		}
		virtual void Update()
		{
			bool is_alive = get_state() != UnitState::S_UNIT_DEAD;
		
			if (is_alive)
			{
				DynamicFunctionalEntity::Update();

				if (get_current_stats().getHp() == 0)
				{
					OnDeath();
				}
			}

		}

	};

}